﻿package plugin.union.game
{
    import cmd.*;
    import com.core.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.union.*;
    import uiresource.union.*;
    import view.appDrag.*;
    import view.message.*;
    import view.mornExtends.*;

    public class UnionCkGame extends UnionCkUI implements IPage, IRoot
    {
        private var _isDown:Boolean = false;
        private var _selectCareer:Boolean = false;
        private var _selectExchange:Boolean = false;
        private var _selfMsg:Object;
        private var _selectTarget:Object;
        private var _storeIndex:int = 0;
        private var arms:Object;
        private var showTips:Boolean = true;

        public function UnionCkGame()
        {
            AppDragMgr.RegisterModule(this);
            btn_jx.toolTip = "<font color=\'#53b436\'>1万金币=150贡献度+150行会资金</font>";
            this.initEvent();
            return;
        }// end function

        private function initEvent() : void
        {
            btn_pre.clickHandler = new Handler(this.toPre);
            btn_next.clickHandler = new Handler(this.toNext);
            btn_jx.clickHandler = new Handler(this.toJx);
            cb_career.clickHandler = new Handler(this.selectCareer);
            cb_exchange.clickHandler = new Handler(this.selectExchange);
            txt_money.addEventListener(Event.CHANGE, this.checkChange);
            btn_destroy.clickHandler = new Handler(this.toDestroy);
            return;
        }// end function

        public function destoryDebug() : void
        {
            var _loc_1:Object = null;
            for each (_loc_1 in list_store.array)
            {
                
                if (_loc_1[4].reinlv <= 0 && _loc_1[4].level <= 60)
                {
                    UnionSender._53destroyCk(_loc_1.index);
                    setTimeout(this.destoryDebug, 1000);
                    return;
                }
            }
            return;
        }// end function

        public function getDebug() : void
        {
            var _loc_1:Object = null;
            for each (_loc_1 in list_store.array)
            {
                
                if (_loc_1[4].reinlv <= 0 && _loc_1[4].level <= 80 && _loc_1[4].superlevel == 0)
                {
                    UnionSender._55getCk(_loc_1.index);
                    setTimeout(this.getDebug, 1000);
                    return;
                }
            }
            return;
        }// end function

        private function toDestroy() : void
        {
            if (img_select.visible)
            {
                if (this._selectTarget == null)
                {
                    WarningManager.show(22);
                    img_select.visible = false;
                }
                else if (this.showTips)
                {
                    Alert2.show("<font color=\'#c72514\'>摧毁后将永远失去</font><br>是否确认销毁装备【<font color=\'#c72514\'>" + this._selectTarget[4].name + "</font>】", "温馨提示", ["确认", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    UnionSender._53destroyCk(_selectTarget.index);
                }
                else if (param1 == 100)
                {
                    showTips = false;
                    UnionSender._53destroyCk(_selectTarget.index);
                }
                return;
            }// end function
            , null, false, true, true);
                }
                else
                {
                    UnionSender._53destroyCk(this._selectTarget.index);
                }
            }
            else
            {
                WarningManager.show(25);
            }
            return;
        }// end function

        protected function checkChange(event:Event) : void
        {
            if (int(txt_money.text) < 10000)
            {
                txt_money.text = "10000";
                WarningManager.show(24);
            }
            return;
        }// end function

        private function selectExchange() : void
        {
            this._selectExchange = cb_exchange.selected;
            list_store.selectedIndex = -1;
            this.showStore();
            return;
        }// end function

        private function selectCareer() : void
        {
            this._selectCareer = cb_career.selected;
            list_store.selectedIndex = -1;
            this.showStore();
            return;
        }// end function

        private function toJx() : void
        {
            var _loc_1:* = int(txt_money.text);
            var _loc_2:* = UserData.getBagData();
            if (int(_loc_2.gold) + _loc_2.bgold < _loc_1)
            {
                WarningManager.show(21);
            }
            else
            {
                UnionSender._37refer([{id:6, count:_loc_1}]);
            }
            return;
        }// end function

        private function toNext() : void
        {
            list_store.selectedIndex = -1;
            img_select.visible = false;
            var _loc_1:* = list_store;
            var _loc_2:* = list_store.page + 1;
            _loc_1.page = _loc_2;
            txt_page.text = (list_store.page + 1) + "/" + list_store.totalPage;
            return;
        }// end function

        private function toPre() : void
        {
            list_store.selectedIndex = -1;
            img_select.visible = false;
            var _loc_1:* = list_store;
            var _loc_2:* = list_store.page - 1;
            _loc_1.page = _loc_2;
            txt_page.text = (list_store.page + 1) + "/" + list_store.totalPage;
            return;
        }// end function

        public function refresh() : void
        {
            var _loc_4:Object = null;
            var _loc_1:* = ModelProxy.getValue("data", "unionInfo");
            var _loc_2:* = _loc_1.memberList;
            var _loc_3:* = UserData.getUserData("rid");
            for each (_loc_4 in _loc_2)
            {
                
                if (_loc_4.rid == _loc_3)
                {
                    this._selfMsg = _loc_4;
                }
            }
            this.showBag();
            this.showStore();
            this.showGongxian();
            return;
        }// end function

        private function showGongxian() : void
        {
            var _loc_1:* = UserData.getBagData("contribution");
            txt_gongxian.text = _loc_1 + "";
            return;
        }// end function

        public function showStore() : void
        {
            var _loc_5:int = 0;
            var _loc_6:Object = null;
            var _loc_7:Array = null;
            var _loc_1:* = ModelProxy.getValue("data", "unionCk");
            var _loc_2:* = new Array(360);
            this._storeIndex = 0;
            var _loc_3:* = UserData.getUserData("career");
            var _loc_4:* = UserData.getBagData("contribution");
            if (_loc_1)
            {
                _loc_5 = 0;
                while (_loc_5 < _loc_2.length)
                {
                    
                    _loc_6 = _loc_1[_loc_5];
                    if (_loc_6)
                    {
                        if (this._selectCareer)
                        {
                            if (_loc_3 != _loc_6.career)
                            {
                                ;
                            }
                        }
                        if (this._selectExchange)
                        {
                            _loc_7 = _loc_6.uniondonate.split("#");
                            if (_loc_4 < _loc_7[0])
                            {
                                ;
                            }
                        }
                        _loc_6.isUnionCk = true;
                        _loc_6.wear = 0;
                        _loc_2[this._storeIndex] = {4:_loc_6, dragFucker:"Store", index:_loc_5};
                        var _loc_8:String = this;
                        var _loc_9:* = this._storeIndex + 1;
                        _loc_8._storeIndex = _loc_9;
                    }
                    _loc_5++;
                }
            }
            this.sortCkArr(_loc_2);
            list_store.array = _loc_2;
            list_store.renderHandler = new Handler(this.itemRander, [list_store, true]);
            txt_page.text = (list_store.page + 1) + "/" + list_store.totalPage;
            img_select.visible = false;
            btn_destroy.visible = this._selfMsg.title >= 10;
            return;
        }// end function

        private function sortCkArr(param1:Array) : void
        {
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            var _loc_5:Object = null;
            var _loc_2:* = param1.length - 1;
            while (_loc_2 >= 0)
            {
                
                _loc_3 = 0;
                while (_loc_3 < _loc_2)
                {
                    
                    _loc_4 = param1[_loc_3];
                    _loc_5 = param1[(_loc_3 + 1)];
                    if (_loc_4 == null && _loc_5 != null)
                    {
                        this.swap(param1, _loc_3, (_loc_3 + 1));
                    }
                    else if (_loc_4 != null && _loc_5 != null)
                    {
                        if (_loc_4[4].reinlv < _loc_5[4].reinlv)
                        {
                            this.swap(param1, _loc_3, (_loc_3 + 1));
                        }
                        else if (_loc_4[4].reinlv == _loc_5[4].reinlv)
                        {
                            if (_loc_4[4].level < _loc_5[4].level)
                            {
                                this.swap(param1, _loc_3, (_loc_3 + 1));
                            }
                            else if (_loc_4[4].level == _loc_5[4].level)
                            {
                                if (_loc_4[4].cls > _loc_5[4].cls)
                                {
                                    this.swap(param1, _loc_3, (_loc_3 + 1));
                                }
                                else if (_loc_4[4].cls == _loc_5[4].cls)
                                {
                                    if (_loc_4[4].career > _loc_5[4].career)
                                    {
                                        this.swap(param1, _loc_3, (_loc_3 + 1));
                                    }
                                    else if (_loc_4[4].career == _loc_5[4].career)
                                    {
                                        if (_loc_4[4].superlevel < _loc_5[4].superlevel)
                                        {
                                            this.swap(param1, _loc_3, (_loc_3 + 1));
                                        }
                                    }
                                }
                            }
                        }
                    }
                    _loc_3++;
                }
                _loc_2 = _loc_2 - 1;
            }
            return;
        }// end function

        public function swap(param1:Array, param2:int, param3:int) : void
        {
            var _loc_4:* = param1[param2];
            param1[param2] = param1[param3];
            param1[param3] = _loc_4;
            return;
        }// end function

        public function showBag() : void
        {
            var _loc_6:Object = null;
            var _loc_7:Object = null;
            var _loc_1:* = UserData.getBagData();
            var _loc_2:* = _loc_1.maxcount;
            var _loc_3:* = new Array(42);
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            while (_loc_5 < _loc_2)
            {
                
                _loc_6 = _loc_1[_loc_5];
                if (_loc_6)
                {
                    _loc_7 = _loc_6[4];
                    if (_loc_7)
                    {
                        if (_loc_7.uniondonate)
                        {
                            _loc_6.dragFucker = "Bag";
                            _loc_3[_loc_4] = _loc_6;
                            _loc_4++;
                        }
                    }
                }
                _loc_5++;
            }
            list_bag.array = _loc_3;
            list_bag.renderHandler = new Handler(this.itemRander, [list_bag, false]);
            return;
        }// end function

        private function itemRander(param1:List, param2:Boolean, param3:Component, param4:int) : void
        {
            var _loc_5:Object = null;
            var _loc_6:FlashItem = null;
            var _loc_7:Image = null;
            var _loc_8:Image = null;
            if (param4 < param1.length)
            {
                _loc_5 = param3.dataSource;
                _loc_6 = param3.getChildByName("fi") as FlashItem;
                _loc_7 = param3.getChildByName("unAvailable") as Image;
                _loc_8 = param3.getChildByName("attrup") as Image;
                var _loc_9:Boolean = false;
                _loc_8.visible = false;
                _loc_7.visible = _loc_9;
                if (_loc_5)
                {
                    _loc_6.data = _loc_5[4];
                    this.checkLimit(_loc_5[4], _loc_7, _loc_8);
                }
                else
                {
                    _loc_6.data = _loc_5;
                    if (_loc_5 != null)
                    {
                        this.checkLimit(_loc_5, _loc_7, _loc_8);
                    }
                }
                _loc_6.changeSuperLvXY(6, 3);
                param3.removeEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownHnalder);
                param3.removeEventListener(MouseEvent.MOUSE_UP, this.mouseUpHnalder);
                param3.removeEventListener(MouseEvent.MOUSE_MOVE, this.moveHandler);
                param3.addEventListener(MouseEvent.MOUSE_DOWN, this.mouseDownHnalder);
                param3.addEventListener(MouseEvent.MOUSE_UP, this.mouseUpHnalder);
                param3.addEventListener(MouseEvent.MOUSE_MOVE, this.moveHandler);
                if (param2)
                {
                    if (this._selfMsg.title >= 10)
                    {
                        param3.removeEventListener(MouseEvent.CLICK, this.storeSelect);
                        param3.addEventListener(MouseEvent.CLICK, this.storeSelect);
                    }
                }
            }
            return;
        }// end function

        public function checkLimit(param1:Object, param2:Image, param3:Image) : void
        {
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            if (param1.career != 4)
            {
                if (param1.career != UserData.getUserData("career"))
                {
                    param2.visible = true;
                    return;
                }
            }
            if (param1.sex != 2)
            {
                if (param1.sex != UserData.getUserData("sex"))
                {
                    param2.visible = true;
                    return;
                }
            }
            var _loc_4:* = param1.cls;
            if (this.arms == null)
            {
                this.arms = ModelProxy.getValue("data", "arms");
            }
            if (_loc_4 == 3 && this.arms[_loc_4] != null)
            {
                if (this.getPowValue(this.arms[_loc_4]) > this.getPowValue(this.arms[10]))
                {
                    _loc_4 = 10;
                }
            }
            else if (_loc_4 == 2 && this.arms[_loc_4] != null)
            {
                if (this.getPowValue(this.arms[_loc_4]) > this.getPowValue(this.arms[9]))
                {
                    _loc_4 = 9;
                }
            }
            var _loc_5:* = this.arms[_loc_4];
            if (this.arms[_loc_4])
            {
                _loc_6 = this.getPowValue(_loc_5);
                _loc_7 = this.getPowValue(param1);
                if (_loc_6 < _loc_7)
                {
                    param3.visible = true;
                }
            }
            else
            {
                param3.visible = true;
            }
            return;
        }// end function

        private function getPowValue(param1:Object) : int
        {
            if (param1 == null || !param1.hasOwnProperty("itemid"))
            {
                return 0;
            }
            return param1.nbvalue;
        }// end function

        protected function storeSelect(event:MouseEvent) : void
        {
            var _loc_2:* = event.currentTarget;
            this._selectTarget = _loc_2.dataSource;
            if (this._selectTarget)
            {
                img_select.visible = true;
                img_select.x = _loc_2.x + 3 + list_store.x;
                img_select.y = (_loc_2.y + 1) + list_store.y;
            }
            return;
        }// end function

        protected function moveHandler(event:MouseEvent) : void
        {
            var _loc_2:Box = null;
            var _loc_3:FlashItem = null;
            var _loc_4:Image = null;
            var _loc_5:BitmapData = null;
            var _loc_6:Bitmap = null;
            if (this._isDown)
            {
                _loc_2 = event.currentTarget as Box;
                _loc_3 = _loc_2.getChildByName("fi") as FlashItem;
                _loc_4 = _loc_3.getChildByName("icon") as Image;
                if (_loc_4 == null || _loc_4.visible == false)
                {
                    return;
                }
                _loc_5 = new BitmapData(42, 42, true, 0);
                _loc_5.draw(_loc_4);
                _loc_6 = new Bitmap(_loc_5);
                AppDragMgr.clingyItem(_loc_3, _loc_2.dataSource, _loc_6, 2, "unionCk" + _loc_2.dataSource.dragFucker);
                this._isDown = false;
            }
            return;
        }// end function

        protected function mouseUpHnalder(event:MouseEvent) : void
        {
            this._isDown = false;
            return;
        }// end function

        protected function mouseDownHnalder(event:MouseEvent) : void
        {
            this._isDown = true;
            return;
        }// end function

        public function DragOver(param1:Object, param2:Point, param3:String) : void
        {
            var _loc_4:Point = null;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:Array = null;
            if (param3 == "unionCkBag")
            {
                _loc_4 = this.globalToLocal(param2);
                if (_loc_4.x > list_store.x && _loc_4.y > list_store.y && _loc_4.x < list_store.x + list_store.width && _loc_4.y < list_store.y + list_store.height)
                {
                    if (this._storeIndex >= 360)
                    {
                        WarningManager.show(26);
                    }
                    else
                    {
                        UnionSender._52setCk(param1.index);
                    }
                }
            }
            else if (param3 == "unionCkStore")
            {
                _loc_4 = this.globalToLocal(param2);
                if (_loc_4.x > list_bag.x && _loc_4.y > list_bag.y && _loc_4.x < list_bag.x + list_bag.width && _loc_4.y < list_bag.y + list_bag.height)
                {
                    _loc_5 = UserData.getBagData("contribution");
                    _loc_6 = UserData.getBagData("emptycount");
                    _loc_7 = param1[4].uniondonate.split("#");
                    if (_loc_5 < _loc_7[0])
                    {
                        WarningManager.show(20, [_loc_7[0], _loc_7[0] - _loc_5]);
                    }
                    else if (_loc_6 <= 0)
                    {
                        WarningManager.show(27);
                    }
                    else
                    {
                        UnionSender._55getCk(param1.index);
                    }
                }
            }
            return;
        }// end function

        public function update(param1:int, param2:Object) : void
        {
            this.arms = null;
            switch(param1)
            {
                case CMDUnion._55TOGETCK:
                {
                    this.showStore();
                    break;
                }
                case UnionPublics.UPDATECONTRIBUTION:
                {
                    this.showGongxian();
                    break;
                }
                case UnionPublics.UPDATEBAG:
                {
                    this.showBag();
                    break;
                }
                case UnionPublics.GETDEBUG:
                {
                    this.getDebug();
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

    }
}
